package org.flexspeedup.command
{
import mx.rpc.AsyncToken;
import mx.rpc.IResponder;
import mx.rpc.http.HTTPService;

import org.flexspeedup.model.Settings;
import org.swizframework.utils.chain.CommandChainStep;

/**
 * Loads the confguration file
 */
public class LoadSettingsCommand extends CommandChainStep  implements IResponder
{

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------
    
    /**
     * Returns a new instance of this class.
     */
    public function LoadSettingsCommand()
    {
        super();
    }
    
    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------
    
    //----------------------------------
    // url
    //----------------------------------
    [Bindable]
    /**
     * The url where the configuration file is located.
     */
    public var url:String;

    //----------------------------------
    // data
    //----------------------------------
    [Bindable]
    /**
     * The XMLList with the configuration values.
     */
    public var data:XMLList;

    //----------------------------------
    // settings
    //----------------------------------
    /**
     * An optional <code>Settings</code> instance to set once the data is loaded.
     */
    public var settings:Settings;
   
    //--------------------------------------------------------------------------
    //
    //  Overridden Methods - CommandChainStep
    //
    //--------------------------------------------------------------------------

    override public function execute():void
    {
        var service:HTTPService = new HTTPService();
        service.resultFormat = "e4x";
        service.url = url;
        
        var token:AsyncToken = service.send();
        token.addResponder(this);
    }
    
    //--------------------------------------------------------------------------
    //
    //  Methods - IResponder
    //
    //--------------------------------------------------------------------------
    
    /**
     *  @inheritDoc mx.rpc.IResponder#result
     */
    public function result(data:Object):void
    {
        this.data = data.result.element as XMLList;
        complete()
    }

    /**
     *  @inheritDoc mx.rpc.IResponder#faule
     */
    public function fault(info:Object):void
    {
        error();
        throw new Error(info.fault);
    }
}
}